const { Transform } = require('stream');
const _ = require('lodash');
const utils = require('../../utils');

module.exports = function () {
  const stream = new Transform({ objectMode: true });
 
  const list = [];
  stream._transform = async function (chunk, encoding, callback) {
    if (_.isArray(chunk)) {
      for (const item of chunk) {
        stream.push(item);
      }
    } else {
      list.push(chunk);
    }
    callback();
  };
  stream.on('finish', function () {
    utils.debug('flatTransform [finish]');
  });

  return stream;
};